home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000068.txt < prev    next >
Text File  |  2013-04-03  |  7KB  |  249 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. cr.define('cr.ui', function() {
  6.  
  7.   /** @const */ var MenuItem = cr.ui.MenuItem;
  8.  
  9.   /**
  10.    * Creates a new menu element. Menu dispatches all commands on the element it
  11.    * was shown for.
  12.    *
  13.    * @param {Object=} opt_propertyBag Optional properties.
  14.    * @constructor
  15.    * @extends {HTMLMenuElement}
  16.    */
  17.   var Menu = cr.ui.define('menu');
  18.  
  19.   Menu.prototype = {
  20.     __proto__: HTMLMenuElement.prototype,
  21.  
  22.     selectedIndex_: -1,
  23.  
  24.     /**
  25.      * Element for which menu is being shown.
  26.      */
  27.     contextElement: null,
  28.  
  29.     /**
  30.      * Initializes the menu element.
  31.      */
  32.     decorate: function() {
  33.       this.addEventListener('mouseover', this.handleMouseOver_);
  34.       this.addEventListener('mouseout', this.handleMouseOut_);
  35.  
  36.       this.classList.add('decorated');
  37.       this.setAttribute('role', 'menu');
  38.       this.hidden = true;  // Hide the menu by default.
  39.  
  40.       // Decorate the children as menu items.
  41.       var children = this.children;
  42.       for (var i = 0, child; child = children[i]; i++) {
  43.         cr.ui.decorate(child, MenuItem);
  44.       }
  45.     },
  46.  
  47.     /**
  48.      * Adds menu item at the end of the list.
  49.      * @param {Object} item Menu item properties.
  50.      * @return {cr.ui.MenuItem} The created menu item.
  51.      */
  52.     addMenuItem: function(item) {
  53.       var menuItem = this.ownerDocument.createElement('menuitem');
  54.       this.appendChild(menuItem);
  55.  
  56.       cr.ui.decorate(menuItem, MenuItem);
  57.  
  58.       if (item.label)
  59.         menuItem.label = item.label;
  60.  
  61.       if (item.iconUrl)
  62.         menuItem.iconUrl = item.iconUrl;
  63.  
  64.       return menuItem;
  65.     },
  66.  
  67.     /**
  68.      * Adds separator at the end of the list.
  69.      */
  70.     addSeparator: function() {
  71.       var separator = this.ownerDocument.createElement('hr');
  72.       this.appendChild(separator);
  73.     },
  74.  
  75.     /**
  76.      * Clears menu.
  77.      */
  78.     clear: function() {
  79.       this.textContent = '';
  80.     },
  81.  
  82.     /**
  83.      * Walks up the ancestors of |el| until a menu item belonging to this menu
  84.      * is found.
  85.      * @param {Element} el The element to start searching from.
  86.      * @return {cr.ui.MenuItem} The found menu item or null.
  87.      * @private
  88.      */
  89.     findMenuItem_: function(el) {
  90.       while (el && el.parentNode != this) {
  91.         el = el.parentNode;
  92.       }
  93.       return el;
  94.     },
  95.  
  96.     /**
  97.      * Handles mouseover events and selects the hovered item.
  98.      * @param {Event} e The mouseover event.
  99.      * @private
  100.      */
  101.     handleMouseOver_: function(e) {
  102.       var overItem = this.findMenuItem_(e.target);
  103.       this.selectedItem = overItem;
  104.     },
  105.  
  106.     /**
  107.      * Handles mouseout events and deselects any selected item.
  108.      * @param {Event} e The mouseout event.
  109.      * @private
  110.      */
  111.     handleMouseOut_: function(e) {
  112.       this.selectedItem = null;
  113.     },
  114.  
  115.     /**
  116.      * The selected menu item or null if none.
  117.      * @type {cr.ui.MenuItem}
  118.      */
  119.     get selectedItem() {
  120.       return this.children[this.selectedIndex];
  121.     },
  122.     set selectedItem(item) {
  123.       var index = Array.prototype.indexOf.call(this.children, item);
  124.       this.selectedIndex = index;
  125.     },
  126.  
  127.     /**
  128.      * Focuses the selected item. If selectedIndex is invalid, set it to 0
  129.      * first.
  130.      */
  131.     focusSelectedItem: function() {
  132.       if (this.selectedIndex < 0 ||
  133.           this.selectedIndex > this.children.length) {
  134.         this.selectedIndex = 0;
  135.       }
  136.  
  137.       if (this.selectedItem)
  138.         this.selectedItem.focus();
  139.     },
  140.  
  141.     /**
  142.      * Menu length
  143.      */
  144.     get length() {
  145.       return this.children.length;
  146.     },
  147.  
  148.     /**
  149.      * This is the function that handles keyboard navigation. This is usually
  150.      * called by the element responsible for managing the menu.
  151.      * @param {Event} e The keydown event object.
  152.      * @return {boolean} Whether the event was handled be the menu.
  153.      */
  154.     handleKeyDown: function(e) {
  155.       var item = this.selectedItem;
  156.  
  157.       var self = this;
  158.       function selectNextAvailable(m) {
  159.         var children = self.children;
  160.         var len = children.length;
  161.         var i = self.selectedIndex;
  162.         if (i == -1 && m == -1) {
  163.           // Edge case when needed to go the last item first.
  164.           i = 0;
  165.         }
  166.  
  167.         // "i" may be negative(-1), so modulus operation and cycle below
  168.         // wouldn't work as assumed. This trick makes startPosition positive
  169.         // without altering it's modulo.
  170.         var startPosition = (i + len) % len;
  171.  
  172.         while (true) {
  173.           i = (i + m + len) % len;
  174.  
  175.           // Check not to enter into infinite loop if all items are hidden or
  176.           // disabled.
  177.           if (i == startPosition)
  178.             break;
  179.  
  180.           item = children[i];
  181.           if (item && !item.isSeparator() && !item.hidden && !item.disabled)
  182.             break;
  183.         }
  184.         if (item && !item.disabled)
  185.           self.selectedIndex = i;
  186.       }
  187.  
  188.       switch (e.keyIdentifier) {
  189.         case 'Down':
  190.           selectNextAvailable(1);
  191.           this.focusSelectedItem();
  192.           return true;
  193.         case 'Up':
  194.           selectNextAvailable(-1);
  195.           this.focusSelectedItem();
  196.           return true;
  197.         case 'Enter':
  198.         case 'U+0020': // Space
  199.           if (item) {
  200.             var activationEvent = cr.doc.createEvent('Event');
  201.             activationEvent.initEvent('activate', true, true);
  202.             activationEvent.originalEvent = e;
  203.             if (item.dispatchEvent(activationEvent)) {
  204.               if (item.command)
  205.                 item.command.execute();
  206.             }
  207.           }
  208.           return true;
  209.       }
  210.  
  211.       return false;
  212.     },
  213.  
  214.     /**
  215.      * Updates menu items command according to context.
  216.      * @param {Node=} node Node for which to actuate commands state.
  217.      */
  218.     updateCommands: function(node) {
  219.       var children = this.children;
  220.  
  221.       for (var i = 0, child; child = children[i]; i++)
  222.         child.updateCommand(node);
  223.     }
  224.   };
  225.  
  226.   function selectedIndexChanged(selectedIndex, oldSelectedIndex) {
  227.     var oldSelectedItem = this.children[oldSelectedIndex];
  228.     if (oldSelectedItem) {
  229.       oldSelectedItem.selected = false;
  230.       oldSelectedItem.blur();
  231.     }
  232.     var item = this.selectedItem;
  233.     if (item)
  234.       item.selected = true;
  235.   }
  236.  
  237.   /**
  238.    * The selected menu item.
  239.    * @type {number}
  240.    */
  241.   cr.defineProperty(Menu, 'selectedIndex', cr.PropertyKind.JS,
  242.       selectedIndexChanged);
  243.  
  244.   // Export
  245.   return {
  246.     Menu: Menu
  247.   };
  248. });
  249.